﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace HandRise.MBHOP.Controls
{
    public partial class ImageDataListitem : Control
    {
        #region 属性
        private Font font = new Font("雅黑", 12, FontStyle.Regular);
        /// <summary>
        /// 文本的字体
        /// </summary>
        public override Font Font
        {
            get
            {
                return font;
            }
            set
            {
                font = value;
            }
        }

        private string text = "";
        /// <summary>
        /// 显示的文本

        /// </summary>
        public override string Text
        {
            get
            {
                return text;
            }
            set
            {
                text = value;
            }
        }

        private Color backColor = Color.White;
        /// <summary>
        /// 背景色
        /// </summary>
        public override Color BackColor
        {
            get
            {
                return backColor;
            }
            set
            {
                if (value != backColor)
                {
                    backColor = value;
                    exitsIcon = false;
                }
            }
        }

        int leftSpace = 0;
        /// <summary>
        /// 文本的左边距
        /// </summary>
        public int LeftSpace
        {
            get
            {
                return leftSpace;
            }
            set
            {
                leftSpace = value;
            }
        }

        int rightSpace = 0;
        /// <summary>
        /// 文本的右边距
        /// </summary>
        public int RightSpace
        {
            get
            {
                return rightSpace;
            }
            set
            {
                rightSpace = value;
            }
        }

        int labelHigh = 0;
        /// <summary>
        /// 标题栏的高度

        /// </summary>
        public int LabelHigh
        {
            get
            {
                return labelHigh;
            }
            set
            {
                labelHigh = value;
            }
        }

        int borderSpace = 0;
        /// <summary>
        /// 边框宽度
        /// </summary>
        public int BorderSpace
        {
            get
            {
                return borderSpace;
            }
            set
            {
                borderSpace = value;
            }
        }

        Image showImage = null;
        /// <summary>
        /// 弹起的图片

        /// </summary>
        public Image ShowImage
        {
            set
            {
                showImage = value;
            }
            get
            {
                return showImage;
            }
        }

 
        internal object dataSource = null;
        /// <summary>
        /// 图标的数据源
        /// </summary>
        public object DataSource
        {
            get
            {
                return dataSource;
            }
        }


        internal ButtonStateEnum itemSelectState = ButtonStateEnum.Up;
        /// <summary>
        /// 图片的按下和弹起状态

        /// </summary>
        internal ButtonStateEnum ItemSelectState
        {
            get
            {
                return itemSelectState;
            }
            set
            {
                itemSelectState = value;
            }
        }

        internal bool textCenter = false;
        /// <summary>
        /// 文本是否居中
        /// </summary>
        internal bool TextCenter
        {
            get
            {
                return textCenter;
            }
            set
            {
                textCenter = value;
            }
        }

        internal bool textShow = true;
        /// <summary>
        /// 文本是否显示
        /// </summary>
        internal bool TextShow
        {
            get
            {
                return textShow;
            }
            set
            {
                textShow = value;
            }
        }

        //文字的显示面积

        private SizeF textSize = new SizeF();

        /// <summary>
        /// 是否已经画图标

        /// </summary>
        internal bool exitsIcon = false;

        /// <summary>
        /// 项创建事件

        /// </summary>
        //internal event EventItemCreateBefore OnItemCreateBefore;
        #endregion

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            //用于弹出框区域空白,当刷新区域跨图标区域和文字区域时才全部刷新，否则只刷新相应的区域
            {
                try
                {
                    //计算图标的位置
                    int IconX = 0;
                    int IconY = 0;
                     IconX=borderSpace;
                    IconY=borderSpace;
                    //画图标
                    e.Graphics.DrawImage(ShowImage,
                    new Rectangle(IconX+2, IconY+2, this.Width-4, this.Height-borderSpace-4),
                    new Rectangle(0, 0, showImage.Width, showImage.Height), GraphicsUnit.Pixel);
                    exitsIcon = true;
                }
                catch { }
            }


        /*    if ((Text != "") && (textShow))
            {
                //计算要画的文本
                string drawStr = GetShowString(e.Graphics);
                //计算居中上边距

                int TextY = 0;
                if (textSize.Height < LabelHigh)
                {
                    TextY =(this.Height-labelHigh)+ (LabelHigh - (int)textSize.Height) / 2;
                }
                if (!textCenter)
                {
                    if (font.Size > 1)
                        e.Graphics.DrawString(drawStr, font, new SolidBrush(this.ForeColor), new RectangleF(leftSpace, TextY, this.Width - rightSpace - leftSpace, textSize.Height));
                }
                else
                {
                    int TextX = 0;
                         TextX = (this.Width - (int)textSize.Width) / 2;
                    e.Graphics.DrawString(drawStr, font, new SolidBrush(this.ForeColor), new RectangleF(TextX, TextY, this.Width - rightSpace - leftSpace, textSize.Height));
                }
            }*/
        }

        private string GetShowString(Graphics g)
        {

            SizeF size = new SizeF();
            StringBuilder sb = new StringBuilder(this.Text);
            size = g.MeasureString(sb.ToString(), font);
            if (size.Width > this.Width -  rightSpace - leftSpace)
            {
                for (int i = 0; i < sb.Length; i++)
                {

                    size = g.MeasureString(sb.ToString(0, i) + "…", font);

                    if (size.Width > this.Width -  rightSpace - leftSpace - Font.Size)
                    {
                        textSize = size;
                        return sb.ToString(0, i) + "…";
                    }
                }
            }
            textSize = size;
            return sb.ToString();
        }
        }
    }

